﻿using UnityEngine;
using UnityEngine.SceneManagement;
using Network;
using System;

public class ProcRoom : Proc
{
    public ProcRoom()
        : base(ProcType.PROC_LOBBY)
    {

    }

    public override void process()
    {
        NetManager.Instance.Tick(Time.deltaTime);
    }

    public override void onBegin(object[] param)
    {
        SceneManager.LoadScene("room");

        RegistUIEvent();
        RegistNetworkEvent();
    }

    public override void onEnd()
    {
        UnregistUIEvent();
        UnregistNetworkEvent();
    }

    void RegistUIEvent()
    {
        UIEventManager.Instance.RegistEventHandler(UIEventName.OnClickCreateRoomButton, createRoom);
        UIEventManager.Instance.RegistEventHandler(UIEventName.OnClickJoinRoomButton, joinRoom);
    }

    void RegistNetworkEvent()
    {
        NetManager.Instance.RegistTcpMsg(OpCode.create_room_toc, onCreateRoom);
        NetManager.Instance.RegistTcpMsg(OpCode.join_room_toc, onJoinRoom);
        
    }

    void UnregistUIEvent()
    {
        UIEventManager.Instance.UnRegistEventHandler(UIEventName.OnClickCreateRoomButton);
        UIEventManager.Instance.UnRegistEventHandler(UIEventName.OnClickJoinRoomButton); 
    }

    void UnregistNetworkEvent()
    {
        NetManager.Instance.UnregistTcpMsg(OpCode.create_room_toc);
        NetManager.Instance.UnregistTcpMsg(OpCode.join_room_toc);
    }


    private void joinRoom(object[] allParams)
    {
        int roomId = int.Parse((string)allParams[0]);
        join_room_tos proto = new join_room_tos();
        proto.room_id = roomId;
        NetManager.Instance.sendTcpMsg(OpCode.join_room_tos, ProtoUtil.SerializeProto(proto));
    }

    private void createRoom(object[] allParams)
    {
        create_room_tos proto = new create_room_tos();
        NetManager.Instance.sendTcpMsg(OpCode.create_room_tos, ProtoUtil.SerializeProto(proto));
    }

    private void onJoinRoom(MsgObject msg)
    {
        join_room_toc joinRoomResult = ProtoUtil.DeserializeProto<join_room_toc>(msg.msgBody);
        if(joinRoomResult.success)
        {
            int roomId = joinRoomResult.room_id;
            uint conv = joinRoomResult.conv;
            NetManager.Instance.UdpConnect(Config.ServerIp, Config.UdpPort, conv);
            //NetManager.Instance.SendUdpMessage(OpCode.first_packet, ProtoUtil.SerializeProto(new first_packet()));
            Mainloop.Instance.ctrl.changeProc(ProcType.PROC_GAME, roomId, false);
        }
    }

    private void onCreateRoom(MsgObject msg)
    {
        create_room_toc createRoomResult = ProtoUtil.DeserializeProto<create_room_toc>(msg.msgBody);
        if(createRoomResult.success)
        {
            int roomId = createRoomResult.room_id;
            uint conv = createRoomResult.conv;
            NetManager.Instance.UdpConnect(Config.ServerIp, Config.UdpPort, conv);
            //NetManager.Instance.SendUdpMessage(OpCode.first_packet, ProtoUtil.SerializeProto(new first_packet()));
            Mainloop.Instance.ctrl.changeProc(ProcType.PROC_GAME, roomId, true);
        }
    }
}
